#include "cadair.h"

static QStringList LIST_NAME = (QStringList()  << "空调" );
static QStringList LIST_OPEN = (QStringList()  << "开" << "打开" );
static QStringList LIST_CLOSE = (QStringList() << "关" << "关闭" );
static QStringList LIST_TEMP = (QStringList() << "温度" << "度" );
static QStringList LIST_WIND = (QStringList() << "风" << "风速");
static QStringList LIST_MODE = (QStringList() << "模式");

static QStringList LIST_EQUAL = (QStringList() << "=" << "设为" << "设置" );
static QStringList LIST_ADD = (QStringList() << "+" << "加" << "高" << "升" << "上调" << "增加");
static QStringList LIST_SUB = (QStringList() << "-" << "减" << "低" << "降" << "下调" << "减小");

//风速: 0,1,2,3,4,5,6		0自动 1~5 档  6 关风
//模式: 0,1,2,3,4,5,6,7		0自动 1热 2冷 3通风 4除湿 5换气 6睡眠 7清爽

static QStringList LIST_WIND_AUTO = (QStringList() << "0" << "自动" << "默认");
static QStringList LIST_WIND_LOW = (QStringList() << "1" << "1档" << "一档" << "低" << "小");
static QStringList LIST_WIND_NORMAL = (QStringList() << "2" << "2档" << "二档" << "中" << "正常");
static QStringList LIST_WIND_HEIGH = (QStringList() << "3" << "3档" << "三档" << "高" << "大");
static QStringList LIST_WIND_PROWER = (QStringList() << "4" << "增强" << "强力" << "超强" << "强劲");

static QStringList LIST_MODE_AUTO = (QStringList() << "0" << "自动");
static QStringList LIST_MODE_HOT = (QStringList() << "1" <<"制热" );
static QStringList LIST_MODE_CLOD = (QStringList() << "2" <<"制冷" );
static QStringList LIST_MODE_BREATH = (QStringList() << "3" << "通风" << "换气");
static QStringList LIST_MODE_WET = (QStringList() << "4" << "除湿");
static QStringList LIST_MODE_SLEEP = (QStringList() << "6" << "睡眠模式");
static QStringList LIST_MODE_FRESH = (QStringList() << "7" << "清爽");

CADAir::CADAir(QObject *parent):CAudioControlDevice(parent){

}

void CADAir::addDevice(DEVICE_OPERATION *d)
{
    _device_list.append(d);
}

void CADAir::clearDevice()
{
    foreach (DEVICE_OPERATION *d, _device_list) {
        d->deleteLater();
    }

    _device_list.clear();
}

DEVICE_OPERATION * CADAir::parseWord(QString word)
{
    QString op,name,value,audio_calc;

    foreach (DEVICE_OPERATION *s, _device_list) {
        if(word.contains(s->_name))
        {
            name = s->_name;
            break;
        }
    }

    foreach (QString s, LIST_OPEN) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_SWITCH;
            value = DEVICE_STATUS_ON;
            break;
        }
    }

    foreach (QString s, LIST_CLOSE) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_SWITCH;
            value = DEVICE_STATUS_OFF;
            break;
        }
    }

    foreach (QString s, LIST_TEMP) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_TEMP;
            value = match_temp_value(word);
            audio_calc = match_add_sub(word);

            break;
        }
    }

    foreach (QString s, LIST_WIND) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_WIND;
            value = match_wind_speed(word);
            break;
        }
    }

    foreach (QString s, LIST_MODE) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_MODE;
            value = match_mode_set(word);
            break;
        }
    }

    if(op.isEmpty() || value.isEmpty())
    {        
        return nullptr;
    }

    if(op == DEVICE_ACTION_SWITCH && name.isEmpty())
    {
        return nullptr;
    }

    qDebug()<< "Audio: found device " << name << " " << op << " " << value << " " << audio_calc;

    DEVICE_OPERATION *device_op = new DEVICE_OPERATION();
    device_op->_audio_name = name;
    device_op->_audio_operation = op;
    device_op->_audio_value = value;
    device_op->_audio_calc = audio_calc;

    if(device_op->_audio_name.isEmpty())
    {
        device_op->_audio_action_dev_all = true;
    }

    return device_op;

}

bool CADAir::actionAudionControl(DEVICE_OPERATION *devOp)
{
    devOp->_match = false;

    foreach (DEVICE_OPERATION *d, _device_list) {
        if(devOp->_audio_action_dev_all)
        {
            devOp->_id_all << d->_id;
        }
        else if(d->_name.contains(devOp->_audio_name) )
        {
            devOp->_id = d->_id;
            devOp->_name = d->_name;
            devOp->_match = true;

            return true;
        }
    }

    return false;
}

QString CADAir::match_temp_value(QString word)
{

    QRegExp rx("[0-9]{1,2}");
    QString value;
    if(rx.indexIn(word) != -1) {
         value = rx.cap(0);
    }

    return value;
}

QString CADAir::match_add_sub(QString word)
{
    foreach (QString s, LIST_ADD) {
        if(word.contains(s))
        {
            return LIST_ADD.at(0);
        }
    }

    foreach (QString s, LIST_SUB) {
        if(word.contains(s))
        {
            return LIST_SUB.at(0);
        }
    }

    return LIST_EQUAL.at(0);
}

QString CADAir::match_wind_speed(QString word)
{
    foreach (QString s, LIST_WIND_LOW) {
        if(word.contains(s))
        {
            return LIST_WIND_LOW.at(0);
        }
    }

    foreach (QString s, LIST_WIND_NORMAL) {
        if(word.contains(s))
        {
            return LIST_WIND_NORMAL.at(0);
        }
    }

    foreach (QString s, LIST_WIND_HEIGH) {
        if(word.contains(s))
        {
            return LIST_WIND_HEIGH.at(0);
        }
    }

    foreach (QString s, LIST_WIND_AUTO) {
        if(word.contains(s))
        {
            return LIST_WIND_AUTO.at(0);
        }
    }

    return "";
}

QString CADAir::match_mode_set(QString word)
{
    foreach (QString s, LIST_MODE_HOT) {
        if(word.contains(s))
        {
            return LIST_MODE_HOT.at(0);
        }
    }

    foreach (QString s, LIST_MODE_CLOD) {
        if(word.contains(s))
        {
            return LIST_MODE_CLOD.at(0);
        }
    }


    foreach (QString s, LIST_MODE_BREATH) {
        if(word.contains(s))
        {
            return LIST_MODE_BREATH.at(0);
        }
    }


    foreach (QString s, LIST_MODE_SLEEP) {
        if(word.contains(s))
        {
            return LIST_MODE_SLEEP.at(0);
        }
    }

    foreach (QString s, LIST_MODE_AUTO) {
        if(word.contains(s))
        {
            return LIST_MODE_AUTO.at(0);
        }
    }

    return "";
}
